package com.winding.plugin.swagger.utils;

import com.google.common.collect.Lists;
import com.thoughtworks.qdox.model.expression.AnnotationValue;
import com.thoughtworks.qdox.model.expression.AnnotationValueList;
import com.winding.plugin.swagger.enums.RequestMethod;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * <p>TODO
 *
 * @author wind
 * @version 1.0
 * @since 2018/12/6
 */
public class TypeUtil {
    public static String API_PARAM_IGNORE_PATTERN = "|.*?hidden";
    public static String API_METHOD_PATTERN = "<p>(.*?)</p>";
    public static String transString(AnnotationValue annotationValue) {
        if (annotationValue == null) {
            return "";
        }
        return annotationValue.getParameterValue().toString().replace("\"","");
    }

    public static Boolean transBoolean(AnnotationValue annotationValue, Boolean flag) {
        if (annotationValue == null) {
            return flag;
        }
        return Boolean.valueOf((String) annotationValue.getParameterValue());
    }

    public static String[] transList(AnnotationValue annotationValue) {

        if (annotationValue == null) {
            return new String[]{};
        }
        if (annotationValue instanceof AnnotationValueList) {
            AnnotationValueList annotationValueList = (AnnotationValueList) annotationValue;
            return annotationValueList.getParameterValue().stream().map(p -> (p != null ? p.toString().replace("\"", "").trim() : p)).toArray(String[]::new);
        } else {
            String a = (String) annotationValue.getParameterValue();
            if (a == null) {
                return new String[]{};
            } else {
                return new String[]{a.replace("\"", "").trim()};
            }

        }
    }

    public static RequestMethod[] requestMethod(AnnotationValue annotationValue) {
        String[] a = transList(annotationValue);
        List<RequestMethod> requestMethods = Lists.newArrayList();
        Stream.of(a).forEach(v ->
                requestMethods.add(RequestMethod.get(v.toUpperCase()))
        );
        return requestMethods.stream().toArray(RequestMethod[]::new);
    }

    public static String buildPath(String... paths) {
        StringBuffer buffer;
        if (paths.length > 0) {
              buffer = new StringBuffer("/" + paths[0]);

        } else {
            buffer = new StringBuffer();
        }
        if (paths != null && paths.length >= 2) {
            for (int i = 1; i < paths.length; i++) {
                if (paths[i] == null || paths[i].length() == 0) {
                    paths[i] = "";
                }
                buffer.append("/").append(paths[i]);
            }
        }
        return buffer.toString().replace("////","/").replace("///","/").replace("//","/");
    }

    public static Method getMethod(String name, List parameters, Map<String, Method> methodMap){
        for (Map.Entry<String,Method> entry:methodMap.entrySet()){


        }
        return null;
    }
}
